{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install -q pyjwt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests, jwt\n",
    "from IPython.display import JSON\n",
    "\n",
    "CATALOG_URL = \"http://lakekeeper:8181/catalog\"\n",
    "MANAGEMENT_URL = \"http://lakekeeper:8181/management\"\n",
    "KEYCLOAK_TOKEN_URL = \"http://keycloak:8080/realms/iceberg/protocol/openid-connect/token\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sign in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Login to Keycloak\n",
    "CLIENT_ID = \"spark\"\n",
    "CLIENT_SECRET = \"2OR3eRvYfSZzzZ16MlPd95jhLnOaLM52\"\n",
    "\n",
    "response = requests.post(\n",
    "    url=KEYCLOAK_TOKEN_URL,\n",
    "    data={\n",
    "        \"grant_type\": \"client_credentials\",\n",
    "        \"client_id\": CLIENT_ID,\n",
    "        \"client_secret\": CLIENT_SECRET,\n",
    "        \"scope\": \"lakekeeper\"\n",
    "    },\n",
    "    headers={\"Content-type\": \"application/x-www-form-urlencoded\"},\n",
    ")\n",
    "response.raise_for_status()\n",
    "access_token = response.json()['access_token']\n",
    "\n",
    "# Lets inspect the token we got to see that our application name is available:\n",
    "JSON(jwt.decode(access_token, options={\"verify_signature\": False}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Creating a Warehouse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.post(\n",
    "    url=f\"{MANAGEMENT_URL}/v1/warehouse\",\n",
    "    headers={\n",
    "        \"Authorization\": f\"Bearer {access_token}\"\n",
    "    },\n",
    "    json={\n",
    "      \"warehouse-name\": \"demo\",\n",
    "      \"storage-profile\": {\n",
    "        \"type\": \"s3\",\n",
    "        \"bucket\": \"examples\",\n",
    "        \"key-prefix\": \"initial-warehouse\",\n",
    "        \"endpoint\": \"http://minio:9000\",\n",
    "        \"region\": \"local-01\",\n",
    "        \"path-style-access\": True,\n",
    "        \"flavor\": \"s3-compat\",\n",
    "        \"sts-enabled\": True\n",
    "      },\n",
    "      \"storage-credential\": {\n",
    "        \"type\": \"s3\",\n",
    "        \"credential-type\": \"access-key\",\n",
    "        \"aws-access-key-id\": \"minio-root-user\",\n",
    "        \"aws-secret-access-key\": \"minio-root-password\"\n",
    "      }\n",
    "    }\n",
    ")\n",
    "response.raise_for_status()\n",
    "JSON(response.json())    \n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
